package com.gaswell.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaswell.entity.Qba01;
import com.gaswell.mapper.FindLastMMapper;
import com.gaswell.mapper.RtuDataMapper;
import com.gaswell.pojo.Diagnosis;
import com.gaswell.pojo.Qj;
import com.gaswell.pojo.RtuData;
import com.gaswell.service.*;
import com.gaswell.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.Future;

@Service
@DS("rtu")
public class RtuDataService2Impl extends ServiceImpl<RtuDataMapper, RtuData> implements RtuDataService2 {

    @Autowired
    private RtuDataMapper rtuDataMapper;
    @Autowired
    private IQba01Service qba01Service;


    @Autowired
    private DiagnosisNewService diagnosisNewService;

    @Autowired
    private QjService qjService;

    @Autowired
    RtuDataService rtuDataService;
    @Autowired
    private FindLastMMapper findLastMMapper;

    @Autowired
    private FindLastMservice findLastMservice;

    @Override
    @Async("threadPoolTaskExecutor")
    public Future<Boolean> findLatestData5_2(FindLastDataVo2 findLastDataVo, List<FindLastDataVo2> list) {
//        System.out.println(findLastDataVo.getJh()+"===============start");
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        if (findLastDataVo != null) {
            String cjsj = findLastDataVo.getCjsj();
            String[] s = StringUtils.split(cjsj, " ");
//              查询最新的日数据
            Qba01 qba01 = qba01Service.getByDateLast(findLastDataVo.getYwbh(), findLastDataVo.getZwbh());
            if (qba01 != null) {
                findLastDataVo.setRbrq(s[0]);
                BigDecimal rcql_b = null;
                BigDecimal rcsl_b = null;
                if (qba01.getRcql() != null)
                    rcql_b = new BigDecimal(decimalFormat.format(qba01.getRcql()));
                if (qba01.getRcsl() != null)
                    rcsl_b = new BigDecimal(decimalFormat.format(qba01.getRcsl()));
                findLastDataVo.setRcql(rcql_b);
                findLastDataVo.setRcsl(rcsl_b);
                findLastDataVo.setRq(qba01.getRq());
                findLastDataVo.setScsj(qba01.getScsj());
            }
            List<Diagnosis> dataByTime = diagnosisNewService.findDataByTime2(findLastDataVo.getYwbh(), findLastDataVo.getZwbh(), cjsj);
            StringBuilder work = new StringBuilder();
            StringBuilder data = new StringBuilder();
            boolean dataNull = false;
            boolean distortion = false;
            boolean deviation = false;
            boolean jy = false;
            boolean dd = false;
            boolean jyfx = false;
            List<String> record = new LinkedList<>();
            List<String> record_type = new LinkedList<>();
            for (Diagnosis diagnosis : dataByTime) {
                record.add(diagnosis.getRecord());
                record_type.add(diagnosis.getCategory());
                if (diagnosis.getCategory().equals("null")) {
                    dataNull = true;
                }
                if (diagnosis.getCategory().equals("distortion")) {
                    distortion = true;
                }
                if (diagnosis.getCategory().equals("deviation")) {
                    deviation = true;
                }
                if (diagnosis.getCategory().equals("jy1") || diagnosis.getCategory().equals("jy2") || diagnosis.getCategory().equals("jy3")) {
                    jy = true;
                }
                if (diagnosis.getCategory().equals("dd1") || diagnosis.getCategory().equals("dd2") || diagnosis.getCategory().equals("dd3") || diagnosis.getCategory().equals("dd4")) {
                    dd = true;
                }
                if (diagnosis.getCategory().equals("jyfx1") || diagnosis.getCategory().equals("jyfx2")) {
                    jyfx = true;
                }
            }

            findLastDataVo.setRecord(record);
            findLastDataVo.setRecord_type(record_type);
            if (jy)
                work.append("积液,");
            if (dd)
                work.append("冻堵,");
            if (jyfx)
                work.append("积液风险,");
            if (!jy && !dd && !jyfx) {
                if (dataNull || distortion || deviation)
//                        work.append("无,");
                    work.append("数据异常，无法诊断,");
                else
                    work.append("正常,");
            }
            if (dataNull)
                data.append("数据丢失,");
            if (distortion)
                data.append("数据失真,");
            if (deviation)
                data.append("数据趋势偏离,");
            if (!dataNull && !distortion && !deviation)
                data.append("正常,");
            work.deleteCharAt(work.toString().length() - 1);
            data.deleteCharAt(data.toString().length() - 1);
            findLastDataVo.setSjzt(data.toString());
            findLastDataVo.setGkzt(work.toString());
            synchronized (list) {
                list.add(findLastDataVo);
            }
        }
//        System.out.println(findLastDataVo.getJh()+"===============end");
        return new AsyncResult<>(true);
    }

    @Override
    @Async("threadPoolTaskExecutor")
    public Future pageForMultiThreadForExcel(LambdaQueryWrapper qba01LambdaQueryWrapper, int now, int size, List list) {
        Page<RtuData> rtuDataPage = new Page<RtuData>(now + 1, size);
        Page<RtuData> page = this.page(rtuDataPage);
        List<RtuData> records = page.getRecords();
        return new AsyncResult<>(records);
    }

    @Override
    public Boolean findLatestData5_3(FindLastDataVo2 findLastDataVo, List<FindLastDataVo2> list) {
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        if (findLastDataVo != null) {
            String cjsj = findLastDataVo.getCjsj();
            String[] s = StringUtils.split(cjsj, " ");
//              查询最新的日数据
            Qba01 qba01 = qba01Service.getByDateLast(findLastDataVo.getYwbh(), findLastDataVo.getZwbh());
            if (qba01 != null) {
                findLastDataVo.setRbrq(s[0]);
                BigDecimal rcql_b = null;
                BigDecimal rcsl_b = null;
                if (qba01.getRcql() != null)
                    rcql_b = new BigDecimal(decimalFormat.format(qba01.getRcql()));
                if (qba01.getRcsl() != null)
                    rcsl_b = new BigDecimal(decimalFormat.format(qba01.getRcsl()));
                findLastDataVo.setRcql(rcql_b);
                findLastDataVo.setRcsl(rcsl_b);
                findLastDataVo.setRq(qba01.getRq());
                findLastDataVo.setScsj(qba01.getScsj());
            }
            List<Diagnosis> dataByTime = diagnosisNewService.findDataByTime2(findLastDataVo.getYwbh(), findLastDataVo.getZwbh(), cjsj);
            StringBuilder work = new StringBuilder();
            StringBuilder data = new StringBuilder();
            boolean dataNull = false;
            boolean distortion = false;
            boolean deviation = false;
            boolean jy = false;
            boolean dd = false;
            boolean jyfx = false;
            List<String> record = new LinkedList<>();
            List<String> record_type = new LinkedList<>();
            for (Diagnosis diagnosis : dataByTime) {
                record.add(diagnosis.getRecord());
                record_type.add(diagnosis.getCategory());
                if (diagnosis.getCategory().equals("null")) {
                    dataNull = true;
                }
                if (diagnosis.getCategory().equals("distortion")) {
                    distortion = true;
                }
                if (diagnosis.getCategory().equals("deviation")) {
                    deviation = true;
                }
                if (diagnosis.getCategory().equals("jy1") || diagnosis.getCategory().equals("jy2") || diagnosis.getCategory().equals("jy3")) {
                    jy = true;
                }
                if (diagnosis.getCategory().equals("dd1") || diagnosis.getCategory().equals("dd2") || diagnosis.getCategory().equals("dd3") || diagnosis.getCategory().equals("dd4")) {
                    dd = true;
                }
                if (diagnosis.getCategory().equals("jyfx1") || diagnosis.getCategory().equals("jyfx2")) {
                    jyfx = true;
                }
            }

            findLastDataVo.setRecord(record);
            findLastDataVo.setRecord_type(record_type);
            if (jy)
                work.append("积液,");
            if (dd)
                work.append("冻堵,");
            if (jyfx)
                work.append("积液风险,");
            if (!jy && !dd && !jyfx) {
                if (dataNull || distortion || deviation)
//                        work.append("无,");
                    work.append("数据异常，无法诊断,");
                else
                    work.append("正常,");
            }
            if (dataNull)
                data.append("数据丢失,");
            if (distortion)
                data.append("数据失真,");
            if (deviation)
                data.append("数据趋势偏离,");
            if (!dataNull && !distortion && !deviation)
                data.append("正常,");
            work.deleteCharAt(work.toString().length() - 1);
            data.deleteCharAt(data.toString().length() - 1);
            findLastDataVo.setSjzt(data.toString());
            findLastDataVo.setGkzt(work.toString());
            synchronized (list) {
                list.add(findLastDataVo);
            }
        }
        return true;
    }


    public static void test(List<String> ll) {
        ll.add("123");
    }

    public static void main(String[] args) {

    }


}
